package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.DutyBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Duty;
import com.sp.scmdb.service.DutyService;

@Controller("dutyAction")
@Scope("prototype")
public class DutyAction extends BaseAction<DutyBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(DutyAction.class);
	
	private DutyService dutyService;

	@Autowired
	public void setDutyService(DutyService dutyService) {
		this.dutyService = dutyService;
	}

	public String findAll() {		
		try{
			List<Duty> depList = dutyService.queryAll();
			entity.setAllList(depList);
			entity.setList(depList.subList(entity.getCurrentRow(), entity.getLastRow()));
			
			this.button("Super.Popup(5,'添加种类','/SCMDB/dutyAction!addInitDuty.action','',400,120)", "Super.editData('/SCMDB/dutyAction!findDuty.action?id=',400,120)", "Super.delData('/SCMDB/dutyAction!delDuty.action?id=')");
		} catch(RuntimeException e){			
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findAll fail!reason:" + e.getMessage());
			return "error";
		}
		return SUCCESS;
	}
	public String addInitDuty(){
		entity.setListDutyLevel(dutyService.queryDutyLevelAll());
		
		return "addSucc";
	}
	/**
	 * 添加职务
	 */
	public String addDuty() {
		try{
			UserBean user = (UserBean) session.get("user");
			String result = dutyService.newDuty(entity, user);
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
			messageSession(result,"类别");
		} catch(RuntimeException e){			
			log.error("addDuty fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("addDuty fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}

	/**
	 * 跳转至职务修改页面
	 */
	public String findDuty() {
		try{
			String depId = request.getParameter("id");
			Duty duty = dutyService.findID(depId);
			entity.setListDutyLevel(dutyService.queryDutyLevelAll());
			if(null != duty){
				entity.setDutyLevel(String.valueOf(duty.getDutyLevelId()));
				entity.setId(String.valueOf(duty.getId()));
				entity.setName(duty.getName());
			} else {
				// 如果职务不存在，返回到查询页面
				return "commitSucc";
			}
		} catch(RuntimeException e){			
			log.error("findDuty fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("findDuty fail!reason:" + e.getMessage());
			return "error";
		}
		return "editSucc";
	}
	
	/**
	 * 更新职务
	 */
	public String editDuty() {
		try{
			UserBean user = (UserBean) session.get("user");
			String result = dutyService.editDuty(entity, user);
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
			messageSession(result,"类别");
		} catch(RuntimeException e){			
			log.error("editDuty fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("editDuty fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
	
	/**
	 * 删除职务
	 */
	public String delDuty() {
		try{
			UserBean user = (UserBean) session.get("user");
			String depId = request.getParameter("id");
			if(StringUtils.isNotEmpty(depId)){
				String result = dutyService.delDuty(depId.split(","), user);
				messageSession(result, "类别");
			}
		} catch(RuntimeException e){			
			log.error("delDuty fail!reason:" + e.getMessage());
			return "fail";
		} catch(Exception e){
			log.error("delDuty fail!reason:" + e.getMessage());
			return "error";
		}
		return "commitSucc";
	}
}
